package com.github.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *  <p>时间轴工具</p>
 *  @author  Crab2Died
 */
public class DateLineUtils {

    /**
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return List<String>
     * <h3>统计时间线按小时转换</h3>
     * <p>将时间段转换成时间线如：beginTime:2017-03-21 07:19:10 endTime:2017-03-21 9:10:11</p>
     * <p>转换时间线结果为:[2017-03-21 07:00:00, 2017-03-21 08:00:00, 2017-03-21 09:00:00]</p>
     * @author  Crab2Died
     */
    static
    public List<String> converDateToHourLine(Date beginTime, Date endTime) {

        SimpleDateFormat formatHour = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat formatSecod = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> hours = new ArrayList<>();
        try {
            /*将时间转换为整点时间*/
            Date begin = formatHour.parse(formatHour.format(beginTime));
            Date end = formatHour.parse(formatHour.format(endTime));
            long s = begin.getTime();
            long e = end.getTime() + 3600 * 1000;
            // 根据时间范围按小时分割时间
            for (; s < e; s = s + 1000 * 60 * 60) {
                hours.add(formatSecod.format(s));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hours;
    }

    /**
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return List<String>
     * <h3>统计时间线按日转换</h3>
     * <p>将时间段转换成时间线如：beginTime:2017-03-19 endTime:2017-03-21</p>
     * <p>转换时间线结果为:[2017-03-19, 2017-03-20, 2017-03-21]</p>
     * @author  Crab2Died
     */
    static
    public List<String> converDateToDayLine(Date beginTime, Date endTime) {

        SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd");
        List<String> days = new ArrayList<>();
        try {
            /*将时间转换为整日计算形式*/
            Date begin = formatDay.parse(formatDay.format(beginTime));
            Date end = formatDay.parse(formatDay.format(endTime));
            long s = begin.getTime();
            long e = end.getTime();
            // 将时间段按天分割
            for (; s <= e; s = s + 1000 * 60 * 60 * 24) {
                days.add(formatDay.format(s));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return List<String>
     * <h3>统计时间线按周转换</h3>
     * <p>将时间段转换成时间线如：beginTime:2016-12-11 endTime:2017-01-11</p>
     * <p>转换时间线结果为:[51/2016, 52/2016, 53/2016, 1/2017, 2/2017]</p>
     * @author  Crab2Died
     */
    static
    public List<String> converDateToWeekLine(Date beginTime, Date endTime) {

        List<String> weeks = new ArrayList<>();

		/*获取开始时间的年份及第几周*/
        Calendar bca = Calendar.getInstance();
        bca.setTime(beginTime);
        int byear = bca.get(Calendar.YEAR);
        int bweek = bca.get(Calendar.WEEK_OF_YEAR);

		/*获取结束时间的年份及第几周*/
        Calendar eca = Calendar.getInstance();
        eca.setTime(endTime);
        int eyear = eca.get(Calendar.YEAR);
        int eweek = eca.get(Calendar.WEEK_OF_YEAR);

		/*外层循环年，内层循环周*/
        out:
        for (int _year = byear; _year <= eyear; _year++) {
            int _week = 1;
            if (_year == byear)
                _week = bweek;

			/*获取当年最大周数*/
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.YEAR, _year);
            int weekOfYear = bca.getActualMaximum(Calendar.WEEK_OF_YEAR);

            for (; _week <= weekOfYear; _week++) {
                if (_year == eyear && _week > eweek)
                    continue out;
                weeks.add(String.format("%s/%s", _week, _year));
            }
        }
        return weeks;
    }

    /**
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return List<String>
     * <h3>统计时间线按月转换</h3>
     * <p>将时间段转换成时间线如：beginTime:2016-10-11 endTime:2017-01-11</p>
     * <p>转换时间线结果为:[10/2016, 11/2016, 12/2016, 1/2017]</p>
     * @author  Crab2Died
     */
    static
    public List<String> converDateToMothLine(Date beginTime, Date endTime) {

        List<String> months = new ArrayList<>();

		/*获取开始时间的年份及第几月*/
        Calendar bca = Calendar.getInstance();
        bca.setTime(beginTime);
        int byear = bca.get(Calendar.YEAR);
        int bmonth = bca.get(Calendar.MONTH);
		
		/*获取结束时间的年份及第几月*/
        Calendar eca = Calendar.getInstance();
        eca.setTime(endTime);
        int eyear = eca.get(Calendar.YEAR);
        int emonth = eca.get(Calendar.MONTH);
		
		/*外层循环年，内层循环月*/
        out:
        for (int _year = byear; _year <= eyear; _year++) {
            int _month = 0;
            if (_year == byear)
                _month = bmonth;
            for (; _month < 12; _month++) {
                if (_year == eyear && _month > emonth)
                    continue out;
                months.add(String.format("%s/%s", _month + 1, _year));
            }
        }
        return months;
    }
}
